﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.VisitionDAL;
using thinger.VisionModels;
using System.Data;
using SASJL;
using Vision.Camera.HK;
using MC;

namespace thinger.VisionDemo
{
    public static class MainConfig
    {
        public static CameraHK camera;
        public static MC.MCLeisai mc;
        //public static RobotBase rbt;
        public static void Init()
        {
            bool rc = true;
            mc = new MC.MCLeisai();
            mc.Init();
            mc.SetDO(1, 0);
            camera = new CameraHK();
            rc = camera.OpenByIP("192.168.66.1", "192.168.66.10");
            if (rc == false)
            {
                MessageBox.Show("相机连接失败");
            }
        }

        public static void UInit()
        {
            camera.CloseCam();
        }
    }
    public static class demoTCP1
    {
        public static bool CurrPutType1;
        public static DateTime startTime;
        public static DateTime endTime;
        public static int total;
        //纠偏报错
        public static bool isFullNG = false;
        public static bool isReToNG = false;
        public static bool isReadWarn = false;
        public static bool isOpneWarn = false;
        //复判是否完成
        public static bool DoReCheck = false;
        public static string chloadPath;//当前托盘拍照位置

        //全局变量
        public static int CurrNumT = 0;//当前托盘拍照位置
        public static int CurrNumNG = 0;//当前托盘NG位置
        public static int CurrNumOK = 0;//当前托盘OK位置
        public static int CurrNumTMax = 0;//当前托盘拍照最大点阵数
        public static int CurrNumNGMax = 0;//当前托盘NG最大点阵数
        public static int CurrNumOKMax = 0;//当前托盘OK最大点阵数
        //
        public static List<RobotData> QUDatas = new List<RobotData>();
        public static List<RobotData> OKDatas = new List<RobotData>();
        public static List<RobotData> NGDatas = new List<RobotData>();

        //获取码 搬运变量
        public static string Coder_1 = string.Empty;
        public static string Coder_2 = string.Empty;
        //打码参数设置
        public static Socket socketDama;
        public static bool connectDama = false;
        public static string damaIP = string.Empty;
        //扫码
        public static Socket socketDuma;
        public static bool connectDuma = false;
        public static string dumaIP = string.Empty;
        public static string dumaCoder = string.Empty;
        //初始化判断  机器人完成后才进行plc
        public static bool inintRobot = false;



        //机器人
        public static Socket socketRobot;
        public static Socket socketRobotClient;
        public static bool connectRobot = false;
        public static string robotString = string.Empty;
        public static string RobotIP;
        //PLC
        public static H5U h5uPLC = new H5U();
        public static bool connectPLC = false;
        public static string plcIP = string.Empty;
        //mes 相机 数据
        public static bool connectMES = false;
        public static bool connectCamera1 = false;
        public static bool connectCamera2 = false;
        public static bool connectData = false;

        //关闭执行
        public static void closeTcp()
        {
            socketDama?.Close();
            socketDuma?.Close();
            socketRobot?.Close();
            try
            {
                if (connectPLC)
                {
                    if (h5uPLC.connectedH5UOK)
                    {
                        h5uPLC.CloseH5U();
                    }
                }

            }
            catch { }


            socketRobotClient?.Close();
        }
        //线程关闭 停止运行相关


        //----     
        public static void WriteTXT_StreamWriter(string pathAName, string txtContent)
        {
            FileStream fs;
            //判断是否已经有了这个文件
            if (!System.IO.File.Exists(pathAName))
            {
                //没有则创建这个文件,创建写入文件，设置文件属性为隐藏
                fs = new FileStream(pathAName, FileMode.Create, FileAccess.Write);
            }
            else
            {
                //调用File类的Delete()方法删除C盘根目录下的Test.txt文本文件
                System.IO.File.Delete(pathAName);
                //fs = new FileStream(pathAName, FileMode.Open, FileAccess.Write);
                fs = new FileStream(pathAName, FileMode.Create, FileAccess.Write);
            }
            File.SetAttributes(@pathAName, FileAttributes.Hidden);
            StreamWriter sw = new StreamWriter(fs);
            try
            {
                sw.WriteLine(txtContent.Trim());//写入值
            }
            finally
            {
                sw.Flush();
                sw.Dispose();
                sw.Close();
                fs.Close();
            }
        }

    }




}
